﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class TAIKHOANDAO
    {
        public DataTable LoadTaiKhoan()
        {
            DataTable dt_taikhoan = new DataTable();
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_BangTK", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt_taikhoan);
            cn.Close();
            return dt_taikhoan;
        }

        public void ThemTaiKhoan(TAIKHOANDTO tkDto)
        {
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_ThemTK",cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id",SqlDbType.Int);
            cmd.Parameters.Add("@matkhau",SqlDbType.VarChar);
            cmd.Parameters.Add("@loaitk",SqlDbType.Int);
            //gán giá trị cho tham số
            cmd.Parameters["@id"].Value = tkDto.Id;
            cmd.Parameters["@matkhau"].Value = tkDto.MatKhau;
            cmd.Parameters["@loaitk"].Value = tkDto.LoaiTK;
            //thực thi câu truy vấn
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public void XoaTaiKhoan(int id)
        {
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_XoaTK", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id", SqlDbType.Int);
            cmd.Parameters["@id"].Value = id;
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public void SuaTaiKhoan(TAIKHOANDTO tkDto)
        {
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_SuaTK", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id", SqlDbType.Int);
            cmd.Parameters.Add("@matkhau", SqlDbType.VarChar);
            cmd.Parameters.Add("@loaitk", SqlDbType.Int);
            //gán giá trị cho tham số
            cmd.Parameters["@id"].Value = tkDto.Id;
            cmd.Parameters["@matkhau"].Value = tkDto.MatKhau;
            cmd.Parameters["@loaitk"].Value = tkDto.LoaiTK;
            //thực thi câu truy vấn
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public DataTable LoadTimKiem(string tukhoa)
        {
            DataTable dt = new DataTable();
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_TimTK", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@tukhoa", SqlDbType.VarChar);

            cmd.Parameters["@tukhoa"].Value = tukhoa;

            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
    }
}
